﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using DotNetNuke;
using DotNetNuke.Common;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Security;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Services.Localization;
using DotNetNuke.Services.FileSystem;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Modules.Communications;

using CharityGamesOnline.Modules.Event;
using CharityGamesOnline.Modules.Common;

namespace CharityGamesOnline.Modules.Game
{
    public partial class LeaderBoard : CGOBase
    {
        protected int eventID = 0;

        public int EventID
        {
            get
            {
                if (eventID == 0)
                {
                    if (ViewState["LeaderBoardEventID"] != null)
                    {
                        eventID = Int32.Parse(ViewState["LeaderBoardEventID"].ToString());
                    }
                    else if ((Request.QueryString["EventID"] != null))
                    {
                        eventID = Int32.Parse(Request.QueryString["EventID"]);
                    }
                }
                return eventID;
            }
            set
            {
                ViewState["LeaderBoardEventID"] = value;
            }
        }

        public int EventDetailTabID
        {
            get
            {
                if (!Null.IsNull(Settings["EventDetailTabID"]))
                {
                    return Int32.Parse(Settings["EventDetailTabID"].ToString());
                }
                else
                    return TabId;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {            
            if (Page.IsPostBack == false)
            {
                lbCurrentDate.Text = string.Format(Localization.GetString("lbCurrentDate", LocalResourceFile), DateTime.Now.ToShortDateString());
                BindData(true);
            }
        }

        protected void dlLeaderBoard_ItemDataBound(object sender, System.Web.UI.WebControls.DataListItemEventArgs e)
        {
            
        }

        protected void cgoPaging_PageChangeEvent(object sender, EventArgs e)
        {
            BindData(false);
        }

        #region Public Function

        public void BindData(int intEventID)
        {
            EventID = intEventID;
            BindData(true);
        }

        #endregion

        #region Private Function

        private void BindData(bool refreshPaging)
        {
            List<GameEnrollmentInfo> lstEnroll = new List<GameEnrollmentInfo>();
            GameEnrollmentController ctrlEnroll = new GameEnrollmentController();
            EventController ctrlEvent = new EventController();
            dlLeaderBoard.Visible = true;
            lbNoResult.Visible = false;
            cgoPaging.Visible = true;
            if (refreshPaging)
            {
                cgoPaging.CurrentPage = 1;
                cgoPaging.TotalCount = 0;       
                cgoPaging.Visible = false;
            }

            if (EventID > 0)
            {
                // get content                 
                EventInfo objEvent = ctrlEvent.GetEvent(PortalId, EventID);
                lbLeaderBoardTitle.Text = string.Format(Localization.GetString("lbLeaderBoardTitle", LocalResourceFile), objEvent.Name);                
                lstEnroll = ctrlEnroll.GetEnrollments(PortalId, EventID, 0, 0, (int)GameEnrollmentInfo.STATUS.COMPLETED,
                                                      refreshPaging, cgoPaging.PageSize, cgoPaging.CurrentPage);                
            }

            if (refreshPaging && lstEnroll.Count > 0 && lstEnroll[0].TotalRowCount > cgoPaging.PageSize)
            {       
                cgoPaging.TotalCount = lstEnroll[0].TotalRowCount;
                cgoPaging.Visible = true;                
            }
            
            dlLeaderBoard.DataSource = lstEnroll;
            dlLeaderBoard.DataBind();

            if (lstEnroll.Count == 0)
            {
                dlLeaderBoard.Visible = false;
                lbNoResult.Visible = true;
            }
        }

        #endregion        
    }
}